/*
 * RBFDownload.c
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 */

#include "RBFCommon.h"
#include "RBFDownload.h"
#include "app_log.h"
#include <string.h>
#include "util.h"

#define xCONFIG_LOG_RBFDOWNLOAD // 模块日志宏定义,使能,模块输出日志

static void RBF_Download_packet_log(uint8_t *packet, uint16_t packetlen)
{
#if defined(CONFIG_LOG_RBFDOWNLOAD)
  DS_app_log_debug("packet(%d) is:", packetlen);
  DS_app_log_hexdump_debug("", packet, packetlen + RBF_MAC_PACKET_LEN_SIZE);
#else
  (void) packet;
  (void) packetlen;
#endif
}

static void RBF_gw_download_payload_log(RBFMacMessageGWDownload_t *msg)
{
#if defined(CONFIG_LOG_RBFDOWNLOAD)
  DS_app_log_debug("RBF_gw_download_payload_log msg is:\r\n");
  DS_app_log_debug("macAdd = 0x%04X\r\n", msg->macAdd);
  DS_app_log_debug("msgSeq = 0x%02X\r\n", msg->msgSeq);
  DS_app_log_debug("msgRetry = 0x%02X\r\n", msg->msgRetry);
  DS_app_log_debug("rssi = 0x%02X\r\n", msg->rssi);
  DS_app_log_debug("qrfH1 = 0x%02X\r\n", msg->qrfH1);
  DS_app_log_debug("qrfH2 = 0x%02X\r\n", msg->qrfH2);
  DS_app_log_debug("keyValue");
  if (msg->keyValueSize == 0 || msg->keyValue == NULL)
    DS_app_log_debug(" = NULL ");
  else
  {
    DS_app_log_debug("(%d)", msg->keyValueSize);
    DS_app_log_hexdump_debug(" = ", msg->keyValue, msg->keyValueSize);
  }
#else
  (void) msg;
#endif
}

static void RBF_node_ack_payload_log(RBFMacMessageNodeAck_t *msg)
{
#if defined(CONFIG_LOG_RBFDOWNLOAD)
  DS_app_log_debug("RBF_node_ack_payload_log msg is:\r\n");
  DS_app_log_debug("battV = 0x%02X\r\n", msg->battV);
  DS_app_log_debug("rssi = 0x%02X\r\n", msg->rssi);
  DS_app_log_debug("temp = 0x%02X\r\n", msg->temp);
  DS_app_log_debug("qrfR1 = 0x%02X\r\n", msg->qrfR1);
  DS_app_log_debug("qrfR2 = 0x%02X\r\n", msg->qrfR2);
  DS_app_log_debug("keyValue ");
  if (msg->keyValueSize == 0 || msg->keyValue == NULL)
    DS_app_log_debug(" = NULL ");
  else
  {
    DS_app_log_debug("(%d)", msg->keyValueSize);
    DS_app_log_hexdump_debug(" = ", msg->keyValue, msg->keyValueSize);
  }
#else
  (void) msg;
#endif
}

/*
 * 网关主动下发payload
 */
// 解包
void RBF_gw_download_payload_packetToMsg(uint8_t *packet, RBFMacMessageGWDownload_t *msg)
{
  msg->macAdd = l_get_be16(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MACADD);
  msg->msgSeq = l_get_u8(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGSEQ);
  msg->msgRetry = l_get_u8(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGRETRY);
  msg->rssi = l_get_u8(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_RSSI);
  msg->qrfH1 = l_get_u8(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH1);
  msg->qrfH2 = l_get_u8(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH2);
  msg->keyValueSize = *packet - RBF_MAC_PACKET_GW_DOWNLOAD_LEN - RBF_TAIL_LENGTH_MAX;
  msg->keyValue = packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_KEYVALUE;
  RBF_gw_download_payload_log(msg);
}
// 组包
void RBF_gw_download_payload_msgToPacket(RBFMacMessageGWDownload_t msg, uint8_t *packet)
{
  l_put_be16(msg.macAdd, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MACADD);
  l_put_u8(msg.msgSeq, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGSEQ);
  l_put_u8(msg.msgRetry, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGRETRY);
  l_put_u8(msg.rssi, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_RSSI);
  l_put_u8(msg.qrfH1, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH1);
  l_put_u8(msg.qrfH2, packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH2);
  if (msg.keyValue == NULL)
    return;
  memmove(packet + RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_KEYVALUE, msg.keyValue, msg.keyValueSize);
  *packet = RBF_MAC_PACKET_GW_DOWNLOAD_LEN + msg.keyValueSize + RBF_TAIL_LENGTH_MAX ;
  RBF_Download_packet_log(packet, *packet);
}

/*
 * 节点应答payload
 */
// 解包
void RBF_node_ack_payload_packetToMsg(uint8_t *packet, RBFMacMessageNodeAck_t *msg)
{
  msg->battV = l_get_u8(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_BATTV);
  msg->rssi = l_get_u8(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_RSSI);
  msg->temp = l_get_u8(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_TEMP);
  msg->qrfR1 = l_get_u8(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR1);
  msg->qrfR2 = l_get_u8(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR2);

  msg->keyValueSize = *packet - RBF_MAC_PACKET_NODE_ACK_LEN - RBF_TAIL_LENGTH_MAX;
  msg->keyValue = packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_KEYVALUE;
  RBF_node_ack_payload_log(msg);
}
// 组包
void RBF_node_ack_payload_msgToPacket(RBFMacMessageNodeAck_t msg, uint8_t *packet)
{
  l_put_u8(msg.battV, packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_BATTV);
  l_put_u8(msg.rssi, packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_RSSI);
  l_put_u8(msg.temp, packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_TEMP);
  l_put_u8(msg.qrfR1, packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR1);
  l_put_u8(msg.qrfR2, packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR2);
  if (msg.keyValue == NULL)
    return;
  memmove(packet + RBF_MAC_PAYLOAD_NODE_ACK_INDEX_KEYVALUE, msg.keyValue, msg.keyValueSize);
  *packet = RBF_MAC_PACKET_NODE_ACK_LEN + msg.keyValueSize + RBF_TAIL_LENGTH_MAX ;
  RBF_Download_packet_log(packet, *packet);
}
